﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.CommonClass;
using ATW.MT.BUS;
using System.Data;
using System.Text;

namespace ATW.MT.SellManage
{
    public partial class CurrencyManage : CommonPage
    {
        bus_D_Currency currency = new bus_D_Currency();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Server.Transfer("../Main/Login.aspx");
                return;
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }

        //数据绑定
        private void BindData()
        {
            try
            {
                string strDiName = txtDiName.Value.Trim();
                DataTable dt = currency.CurrencyPagerQuery(strDiName, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
                if (dt != null && dt.Rows.Count > 0)
                {
                    AspNetPager1.RecordCount = Convert.ToInt32(dt.Rows[0]["totalpagesize"]);
                }
                else
                {
                    AspNetPager1.RecordCount = 0;
                }
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }

        }

        //查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void AspNetPager1_pageChanged(object sender, EventArgs e)
        {
            BindData();
        }


        //编辑界面 确认按钮事件
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strDCID = "";
                string strName = "";
                decimal delRate = 0;

                //根据主键是否为空，判断为新增或者修改
                if (string.IsNullOrWhiteSpace(txtDCID.Value))
                {
                    strName = txtName.Value;
                    delRate = Convert.ToDecimal(txtRate.Value);

                    int result = currency.AddCurrency(strName, delRate);
                    if (result > 0)
                    {
                        Tips("新增成功");
                        Clear();
                        BindData();
                    }
                    else
                    {
                        Tips("新增失败");
                        return;
                    }
                }
                else
                {
                    strDCID = txtDCID.Value;
                    strName = txtName.Value;
                    delRate = Convert.ToDecimal(txtRate.Value);



                    int result = currency.ModifyItem(strDCID, strName, delRate);
                    if (result > 0)
                    {
                        Tips("修改成功");
                        Clear();
                        BindData();
                    }
                    else
                    {
                        Tips("修改失败");
                        return;
                    }
                }


            }
            catch (Exception ex)
            {
                //return "操作失败:" + ex.Message;
            }
        }

        //修改
        protected void lbnModify_Click(object sender, EventArgs e)
        {
            LinkButton lbn = (LinkButton)sender;
            string strId = lbn.CommandArgument;
            try
            {
                DataTable dtResult = currency.SelectItem(strId);

                if (dtResult.Rows.Count > 0)
                {
                    this.txtDCID.Value = dtResult.Rows[0]["DCID"].ToString();
                    this.txtName.Value = dtResult.Rows[0]["dcName"].ToString();
                    this.txtRate.Value = dtResult.Rows[0]["rate"].ToString();
                    
                }

                StringBuilder sb = new StringBuilder();

                sb.Append("<script language='javascript'>");

                sb.Append("var divAdd = new jSharp.popUpDiv('divAdd');function Show(){divAdd.Show()} Show(); $('#hideAdd').click(function () {divAdd.Close();})");

                sb.Append("</script>");

                ClientScript.RegisterStartupScript(this.GetType(), "LoadPicScript", sb.ToString());

            }
            catch (Exception ex)
            {

            }
        }


        private void writeRes(string s)
        {
            Response.Clear();
            Response.Write(s);
            Response.End();
        }
        //清空控件
        private void Clear()
        {
            txtDCID.Value = "";
            txtName.Value = "";
            txtRate.Value = "";
            
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}